package usercenter

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
)

var _ UserPayAccountAddressModel = (*customUserPayAccountAddressModel)(nil)

type (
	// UserPayAccountAddressModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserPayAccountAddressModel.
	UserPayAccountAddressModel interface {
		userPayAccountAddressModel
		Find(ctx context.Context, uid int64, pageNo int64, pageSize int64) ([]*UserPayAccountAddress, error)
	}

	customUserPayAccountAddressModel struct {
		*defaultUserPayAccountAddressModel
		genSql *gensql.GenSql
	}
)

// NewUserPayAccountAddressModel returns a model for the database table.
func NewUserPayAccountAddressModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserPayAccountAddressModel {
	return &customUserPayAccountAddressModel{
		defaultUserPayAccountAddressModel: newUserPayAccountAddressModel(conn, c, opts...),
		genSql:                            gensql.NewGenSql(),
	}
}

func (m *customUserPayAccountAddressModel) Find(ctx context.Context, uid int64, pageNo int64, pageSize int64) ([]*UserPayAccountAddress, error) {

	query := fmt.Sprintf("select %s from %s ", userPayAccountAddressRows, m.table)
	m.genSql.InitSql(query)
	q, v, err := m.genSql.Where("uid = ?", uid).OrderBy("id desc").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize)).ToSql()
	if err != nil {
		return nil, err
	}

	var resp []*UserPayAccountAddress
	err = m.QueryRowsNoCacheCtx(ctx, &resp, q, v...)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
